package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {

	@Reference
	private UserService userService;


	@RequestMapping("/doSuccess")
	public String doSuccess(){
		return "redirect:http://localhost:83/pages/main.html";
	}

	@RequestMapping("/doFail")
	public String doFail(){
		return "redirect:http://localhost:83/login.html";
	}


	@RequestMapping("/getUsername")
	@ResponseBody
	public Result getUsername(){

		SecurityContext context = SecurityContextHolder.getContext();
		Authentication authentication = context.getAuthentication();
		Object principal = authentication.getPrincipal();
		if(principal instanceof User){

			User u = (User) principal;
			return new Result(true,MessageConst.GET_USERNAME_SUCCESS,u.getUsername());


		}else {
			log.debug("未登录,获取到匿名对象");
			return  new Result(false,"未登录,获取到匿名对象");
		}



	}



}
